import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:shenling_flutter/utils/auth.dart';
import 'package:shenling_flutter/utils/dio.dart';
import 'package:shenling_flutter/utils/event_bus.dart';
import 'package:shenling_flutter/utils/toastMsg.dart';

class Loginpage extends StatefulWidget {
  const Loginpage({super.key});

  @override
  State<Loginpage> createState() => _LoginpageState();
}

class _LoginpageState extends State<Loginpage> {
  final TextEditingController _textController = TextEditingController();
  final TextEditingController _pwdController = TextEditingController();
  //密码状态控制器
  bool _isShowPwd = false;

  @override
  void initState() {
    super.initState();
    _textController.text = 'myAdimin';
    _pwdController.text = '123456lt';
  }

  onSubmit() async {
    final account = _textController.text;
    final password = _pwdController.text;
    if (account.isEmpty || password.isEmpty) {
      Toastmsg.showError('请输入账号或密码');
      return;
    }
    try {
      final res = await http.request(
          'post',
          '/driver/login/account',
          {
            'account': account,
            'password': password,
          },
          null);
      // Toastmsg.showSuccess(res['data']);
      // 保存token
      await Auth().setToken(res['data']);
      Toastmsg.showSuccess('登录成功');

      // 使用 addPostFrameCallback 延迟导航操作
      WidgetsBinding.instance.addPostFrameCallback((_) {
        //返回到首页
        Navigator.pushNamedAndRemoveUntil(context, '/', (route) => false);
      });
    } catch (e) {
      Toastmsg.showError(e.toString());
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('神领快递'),
        backgroundColor: Colors.white,
        centerTitle: true,
        // 去掉返回按钮
        automaticallyImplyLeading: false,
      ),
      body: Container(
        padding: const EdgeInsets.all(20),
        color: Colors.white,
        child: Column(
          children: [
            //登录title
            const Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  '账号登录',
                  style: TextStyle(fontSize: 20),
                ),
                Row(
                  children: [
                    Text(
                      '手机号登录',
                      style: TextStyle(
                        color: Color.fromRGBO(242, 117, 104, 1),
                      ),
                    ),
                    Icon(
                      Icons.arrow_right,
                      color: Color.fromRGBO(242, 117, 104, 1),
                    )
                  ],
                )
              ],
            ),
            const SizedBox(
              height: 20,
            ),
            //输入框-用户名
            TextField(
              controller: _textController,
              decoration: const InputDecoration(
                  hintText: '请输入用户名',
                  hintStyle: TextStyle(color: Color.fromRGBO(194, 192, 192, 1)),
                  //拉高输入框的高度
                  contentPadding: EdgeInsets.only(top: 20, bottom: 20)),
            ),
            //密码
            Stack(
              children: [
                TextField(
                  controller: _pwdController,
                  decoration: const InputDecoration(
                      hintText: '请输入密码',
                      hintStyle:
                          TextStyle(color: Color.fromRGBO(194, 192, 192, 1)),
                      contentPadding: EdgeInsets.only(top: 20, bottom: 20)),
                  //控制密码是否可见
                  obscureText: !_isShowPwd,
                  //显示隐藏密码图标按钮
                ),
                Positioned(
                  right: 0,
                  bottom: 10,
                  child: GestureDetector(
                    onTap: () {
                      setState(() {
                        _isShowPwd = !_isShowPwd;
                      });
                    },
                    child: _isShowPwd
                        ? const Icon(Icons.remove_red_eye)
                        : Image.asset(
                            'assets/imgs/eye_close.png',
                            width: 24,
                            height: 24,
                            color: Colors.black,
                          ),
                  ),
                )
              ],
            ),
            const SizedBox(height: 40),
            //登录按钮
            Container(
              width: MediaQuery.of(context).size.width,
              margin: const EdgeInsets.only(left: 20, right: 20),
              child: ElevatedButton(
                style: ElevatedButton.styleFrom(
                  backgroundColor: const Color.fromRGBO(239, 79, 63, 1),
                  padding: const EdgeInsets.all(15),
                ),
                onPressed: () {
                  // Navigator.pushNamed(context, '/home');
                  onSubmit();
                },
                child: const Text(
                  '登录',
                  style: TextStyle(color: Colors.white),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
